<?php
namespace app\admin\controller;
require '../vendor/autoload.php';

use Gregwar\Captcha\CaptchaBuilder;
use think\Db;
use think\Controller;
use Think\Verify;
class Login extends Controller
{
    //登陆首页
    public function login()
    {
        return view('login');
    }
    //登陆首页
    public function index()
    {
        return view('index');
    }

    //检验用户名，密码，验证码是否正确
    public function check(){
        //判断验证码是否正确
        $captcha1 = $_POST['captcha'];
        $captchas = cookie('captchas');
        if($captcha1!=$captchas){
            return json(-1);
        }
        //判断输入的用户名和密码是否正确
        $username = $_POST['username'];
        $password = $_POST['password'];

        $arr['username'] = array('eq',$username);
        $arr['password'] = array('eq',$password);
        $re = Db::table('admin')->where($arr)->find();
        if(empty($re)){
            return json(-2);
        }else{
            //清空验证码
            cookie('captchas','');
            //保持用户信息
            logins($re);
            return json(1);
        }
    }

    //生成验证码
    public function captchas(){
        $builder = new CaptchaBuilder;
        $captchas = $builder->getPhrase();
        //保持到cookie中
        cookie('captchas',$captchas);
        header( 'Content-type: image/jpeg' );
        $builder->build()->output();
    }

}
